home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / java / jdirecttalker example / mrjtalktest.java < prev   
Encoding:
Java Source  |  2000-06-23  |  3.0 KB  |  146 lines

  1. /**
  2.  *     File:        MRJTalkTest.java
  3.  *
  4.  *    Contains:    Standalone application example that interfaces to MRJTalker native class.
  5.  *
  6.  *    Version:    1.05
  7.  *
  8.  *    Technology:    System 7.6.1 or later
  9.  *    
  10.  *    Package:    Mac OS Runtime For Java™ SDK 2.0.1
  11.  *
  12.  *    Copyright:    © 1984-1998 by Apple Computer, Inc.
  13.  *                All rights reserved.
  14.  *
  15.  *    Bugs?:        If you find a problem with this file, report them to mrj_bugs.
  16.  *                Include the file and version information (from above)
  17.  *                in the problem description and send to:
  18.  *                    Internet:    mrj_bugs@apple.com
  19.  */
  20.  
  21. import java.awt.*;
  22. import java.util.Vector;
  23.  
  24. public
  25. class MRJTalkTest extends Frame implements Runnable {
  26.     /**
  27.      * main for the test
  28.      */
  29.     public static void main() {
  30.         MRJTalkTest sample = new MRJTalkTest();
  31.  
  32.         // add the strings        
  33.         sample.addString("One Ring to rule Them all,");
  34.         sample.addString("One Ring to find Them.");
  35.         sample.addString("One Ring to bring them all,");
  36.         sample.addString("and in the Darkness bind Them,");
  37.         sample.addString("In the Land of Mordor, where the Shadows lie.");
  38.         
  39.         // show the window
  40.         sample.show();
  41.     }
  42.     
  43.     /**
  44.      * Instance Data
  45.      */
  46.     Label itsLabel;
  47.     Vector itsStrings = new Vector();
  48.     Thread itsThread;
  49.     MRJTalker itsTalker;
  50.     
  51.     /**
  52.      * our constructor for this sample
  53.      */
  54.     
  55.     public MRJTalkTest() {
  56.         setTitle("Standalone Application");
  57.         setBackground(Color.lightGray);
  58.         setLayout(new BorderLayout());
  59.         add("Center", itsLabel = new Label("Initial Label", Label.CENTER));
  60.         itsLabel.setFont(new Font("Geneva", Font.PLAIN, 12));
  61.         reshape(100, 100, 300, 60);
  62.         
  63.         // try to construct a talker - if one isn't availiable, getTalker will return null
  64.         itsTalker = MRJTalker.getTalker();
  65.     }
  66.     
  67.     /**
  68.      * When our window is shown, we start a timer thread
  69.      */
  70.      
  71.     public void show() {
  72.         super.show();
  73.         itsThread = new Thread(this);
  74.         itsThread.setPriority(2);
  75.         itsThread.start();
  76.     }
  77.     
  78.     /**
  79.      * When our window is hidden, we stop our timer
  80.      */
  81.  
  82.     public void hide() {
  83.         itsThread.stop();
  84.         itsThread = null;
  85.         super.hide();
  86.     }
  87.     
  88.     /**
  89.      * Add a string to the strings we display
  90.      */
  91.      
  92.     void addString(String s) {
  93.         itsStrings.addElement(s);
  94.     }
  95.  
  96.     /**
  97.      * Handle an event - we only do window destroys here
  98.      */
  99.      
  100.     public boolean handleEvent(Event evt) {
  101.         if (evt.id == Event.WINDOW_DESTROY) {
  102.             hide();
  103.             System.exit(0);
  104.         }
  105.         return false;
  106.     }
  107.     
  108.     /**
  109.      * Our periodic update thread
  110.      */
  111.  
  112.     private boolean snooze(int time) {
  113.         try {
  114.             Thread.sleep(time);
  115.         } catch (InterruptedException e) {
  116.             return false;
  117.         }
  118.         return true;
  119.     }
  120.     
  121.     public void run() {
  122.         int ixString = 0;
  123.         while (itsThread != null) {
  124.             if (! snooze(500))    
  125.                 break;
  126.             
  127.             synchronized (itsStrings) {
  128.                 int ctStrings = itsStrings.size();
  129.                 if (ctStrings > 0) {
  130.                     if (ixString >= ctStrings) {
  131.                         ixString = 0;
  132.                         snooze(1000);
  133.                     }
  134.                     String s = (String) itsStrings.elementAt(ixString++);
  135.                     if (s != null) {
  136.                         itsLabel.setText(s);
  137.                         if (itsTalker != null)
  138.                             itsTalker.speakString(s);
  139.                     }
  140.                 }
  141.             }
  142.         }
  143.         hide();
  144.     }
  145. };
  146.